package com.information.manage.basic.common.utils;

import org.apache.commons.lang3.StringUtils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * 解密前端请求传入的AES携带IV加密后的密码
 * function en(p){var i=CryptoJS.lib.WordArray.random(16);var e=CryptoJS.AES.encrypt(p,CryptoJS.enc.Utf8.parse(hive),{iv:i,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.Pkcs7});return {e:e.toString(),v: i};}
 *  var { e, v } = en(theForm.password.value);
*                 let body = {};
*                 body.password = e;
*                 body.iv = v.toString(CryptoJS.enc.Base64);
 */
public class AESIVUtil {

    private static final String SECRET_KEY = "secretinfomanage"; // 替换为与加密时相同的密钥
    private static final String ALGORITHM = "AES/CBC/PKCS5Padding"; // AES算法/CBC模式/PKCS5Padding填充

    public static String decrypt(String encryptedPassword, String base64EncodedIv) {
        // 将Base64编码的IV解码为字节数组
        byte[] ivBytes = Base64.getDecoder().decode(base64EncodedIv);
        IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);

        // 创建密钥规格
        SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(StandardCharsets.UTF_8), "AES");
        try {
            // 初始化Cipher对象为解密模式
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);

            // 将Base64编码的加密密码解码为字节数组
            byte[] encryptedBytes = Base64.getDecoder().decode(encryptedPassword);

            // 执行解密操作
            byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

            // 将解密后的字节数组转换回字符串
            return new String(decryptedBytes, StandardCharsets.UTF_8);
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return StringUtils.EMPTY;
    }

    public static void main(String[] args) {
        try {
            String encryptedPwd = "x7nN7Dwp9MuLSQCUzcHoTOCcofTmSKjw9b/dbx4IKaQ=";
            // 注意：这里的iv需要从前端接收(转换IV.toString(CryptoJS.enc.Base64))，确保与加密时使用的IV相同
            String iv = "LzffJ7CWfjfQCQQG01648w==";
            String originalPassword = decrypt(encryptedPwd, iv);
            System.out.println("解密后的密码: " + originalPassword);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
